<?php

require_once 'SmartL/Zend/Controller/Action.php';
require_once 'Db/Shop/Record/Order.php';
require_once 'Db/Shop/Record/BasketItem.php';
require_once 'Db/Shop/Record/User.php';
require_once 'Db/Shop/Record/Product.php';
require_once 'Db/Shop/Record/OrderItem.php';
require_once 'SmartL/Zend/Form.php';
require_once 'SmartL/Zend/Paginator/Adapter/Doctrine.php';
require_once 'Zend/Paginator.php';

class Admin_OrdersController extends SmartL_Zend_Controller_Action
{
    public function listAction ()
    {
        // Parametry: stránka = 1
        $query = new Doctrine_Query();
        $dql = $this->config->getAction("list")->getSelection("list")->getDql("Db_Shop_Record_Order");
        $query->parseDqlQuery($dql)->orderBy('t.timecreated DESC');
        if ($this->_hasParam('userid')) {
            $query->where('u.id=?', $this->_getParam('userid'));
        }
		$orders = new Zend_Paginator( new SmartL_Zend_Paginator_Adapter_Doctrine($query, Doctrine::HYDRATE_RECORD) );
		$orders->setCurrentPageNumber($this->_getParam('page'));
		$orders->setItemCountPerPage(2);
        $this->view->orders = $orders;
    }
    public function orderDetailAction ()
    {
        // Parametry: id objednávky
        if ($this->_hasParam('id')) {
            $this->view->form = $form = $this->_getOrderStatusForm();
            $query = new Doctrine_Query();
            $order = $query->from('Db_Shop_Record_Order o')->select('*')->leftJoin('o.User u')->where('o.id = ?', $this->_getParam('id'))->execute()->getFirst();
            if (empty($order)) {
    			$this->flashMessenger->addMessage('invalidId');            	
            	$this->_helper->redirector->goto("list", "Orders");
            }
            
            $this->view->order = $order;            
            $order_totalPrice = 0;
            foreach ($order->Items as $item) {
                $order_totalPrice += $item->amount * $item->price;
            }
            $this->view->order_totalPrice = $order_totalPrice;
            $form->selectStatus->setValue(array_search($order->status, $this->_getStatusEnum()));
            $form->orderId->setValue($this->_getParam('id'));
        } else {
        	$this->flashMessenger->addMessage('missingId');
            $this->_helper->redirector->goto("list", "Orders");
        }
    }
    public function changeOrderStatusAction ()
    {
        // Parametry: id objednávky, stav
        $form = $this->_getOrderStatusForm();
        if ($form->isValid($_POST)) {
            $values = $form->getValues($_POST);
            $query = new Doctrine_Query();
            $order = $query->from('Db_Shop_Record_Order o')->select('o.id')->where('o.id=?', $values['orderId'])->execute()->getFirst();
            
            if (empty($order)) {
    			$this->flashMessenger->addMessage('invalidId');            	
            	$this->_helper->redirector->goto("list", "Orders");
            }            
            
            $order->status = $values['selectStatus'];
            $order->save();
        }
        
        $this->_helper->redirector->gotoRoute(array("id" => $values['orderId'],"action"=>"order-detail","controller"=> "orders"), "admin");
    }
    public function removeOrderAction ()
    {
        // Parametry: id objednávky
        if ($this->_hasParam('id')) {
            $query = new Doctrine_Query();
            $query->delete()->from('Db_Shop_Record_Order o')->where('o.id = ?', $this->_getParam('id'))->execute();
        } else {
        	$this->flashMessenger->addMessage('missingId');
        }
        
        $this->_helper->redirector->gotoRoute(array("controller"=>"orders","action"=>"list"),"admin");
    }
    private function _getOrderStatusForm ()
    {
        $action = $this->view->url(array("controller"=>"orders","action"=>"change-order-status"));
        $config = array(
            "name" => "status",
            "method" => "post",
            "action" => $action,
            "elements" => array(
                "selectStatus" => array(
                    "type" => "select",
                    "options" => array("label" => "status")
                ),
                "orderId" => array("type" => "hidden"),
                "submit" => array(
                    "type" => "submit",
                    "options" => array(
                        "label" => "Send"
        ))));
		$form = SmartL_Zend_Form::getForm($config, $this->translations['status']);
        $form->selectStatus->options = $this->_getStatusEnum();
        return $form;
    }
    private function _getStatusEnum ()
    {
        return array(1 => 'Waiting' , 2 => 'Accepted' , 3 => 'Executed' , 4 => 'Cancelled');
    }
}

